
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "convert.h"

TIMECONVER g_TimeConver;

EXTERN_C void GetDefaultPathA(/* out */ char *szPath)
{
    char *szFilePath;
    szFilePath = getenv("EMULATOR_RESOURCE");
    if (NULL == szFilePath) {
        *szPath = 0;
        return;
    }

    strcpy(szPath, szFilePath);
    strcat(szPath, "\\");
}

EXTERN_C bool IsAlarm()
{
    return g_TimeConver.isAlarm;
}

EXTERN_C void SetIsAlarm(bool flag)
{
    g_TimeConver.isAlarm = flag;
}

EXTERN_C void GetAlarmTime(SystemTime *time)
{
    time->seconds = g_TimeConver.m_alarmSeconds;
    time->microseconds = 0;
}

EXTERN_C void SetAlarmTime(SystemTime time)
{
    g_TimeConver.m_alarmSeconds = time.seconds;
}


EXTERN_C void SaveVMtimeInFile(void)
{
    char szLine[_MAX_LINE];
    memset(szLine, 0, _MAX_LINE);

    FILE *pLogFile;
    char szFileName[_MAX_LINE];

    GetDefaultPathA(szFileName);
    strcat(szFileName, "timeDiff.log");
    pLogFile = fopen(szFileName, "wt");
    if(NULL == pLogFile){
        printf("Can not open the VM time log in timeDiff.log\n");
        return;
    }

    if (fseek(pLogFile, 0, SEEK_SET)) {
        printf("Error in seek timeDiff.log file!\n");
        return;
    }

    fputs ("[TIMEDIFF]\n", pLogFile);
    sprintf(szLine, "seconds=%d, ispositive=%d \n",
                    g_TimeConver.m_Seconds, g_TimeConver.m_isPositive);
    fputs (szLine, pLogFile);
    fputs ("[TIMEALARM]\n", pLogFile);
    sprintf(szLine, "alarmSeconds=%d\n",
                    g_TimeConver.m_alarmSeconds);
    fputs (szLine, pLogFile);
    fputs ("[TIMEZONEDIFF]\n", pLogFile);
    sprintf(szLine, "bias=%d\n", g_TimeConver.Bias);
    fputs (szLine, pLogFile);

    fclose(pLogFile);
    return;
}

EXTERN_C void ReadVMtimeFromFile(void)
{
    char szLine[_MAX_LINE];
    memset(szLine, 0, _MAX_LINE);
    char* szKey;

    FILE *pLogFile;
    char szFileName[_MAX_LINE];

    g_TimeConver.m_Seconds = 0;
    g_TimeConver.m_isPositive = 1;
    g_TimeConver.m_alarmSeconds = 0;
    g_TimeConver.Bias = 0;

    GetDefaultPathA(szFileName);
    strcat(szFileName, "timeDiff.log");
    pLogFile = fopen(szFileName, "rt");
    if(NULL == pLogFile){
        printf("Can not open the VM time log in timeDiff.log\n");
        return;
    }

    if (fseek(pLogFile, 0, SEEK_SET)) {
        printf("Error in seek timeDiff.log file!\n");
        fclose(pLogFile);
        return;
    }

    while (NULL != fgets(szLine, _MAX_LINE, pLogFile)) {
        if (NULL != strstr(szLine, "[TIMEDIFF]")) {
            if(NULL == fgets(szLine, _MAX_LINE, pLogFile)){
                goto _ERROUT;
            };
            szKey = strstr(szLine, "seconds=");
            if(NULL == szKey){
                goto _ERROUT;
            }
            g_TimeConver.m_Seconds = atol((szKey + strlen("seconds=")));
            szKey = strstr(szLine, "ispositive=");
            if(NULL == szKey){
                goto _ERROUT;
            }
            g_TimeConver.m_isPositive = atol((szKey + strlen("ispositive=")));
            continue;
        }
        if (NULL != strstr(szLine, "[ALARMTIME]")) {
            if(NULL == fgets(szLine, _MAX_LINE, pLogFile)) {
                goto _ERROUT;
            }
            szKey = strstr(szLine, "alarmSeconds=");
            if(NULL == szKey){
                goto _ERROUT;
            }
            g_TimeConver.m_alarmSeconds = (unsigned long)atol((szKey + strlen("alarmSeconds=")));
            continue;
        }
        if (NULL != strstr(szLine, "[TIMEZONEDIFF]")) {
            if(NULL == fgets(szLine, _MAX_LINE, pLogFile)){
                goto _ERROUT;
            }
            szKey = strstr(szLine, "bias=");
            if(NULL == szKey){
                goto _ERROUT;
            }
            g_TimeConver.Bias = atol((szKey + strlen("bias=")));
            continue;
        }
    }
    fclose(pLogFile);
    return;

_ERROUT:
    fclose(pLogFile);
    g_TimeConver.m_Seconds = 0;
    g_TimeConver.m_isPositive = 1;
    g_TimeConver.m_alarmSeconds = 0;
    g_TimeConver.Bias = 0;

}

